#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <deque>
using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> a(n + 1), b(n + 1);
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) cin >> b[i];
	deque<int> ap, bp;
	int p; //找最大值i
	for (p = 1; p <= n; p++)
	{
		while (!ap.empty() && a[p] <= ap.back()) ap.pop_back();
		while (!bp.empty() && b[p] <= bp.back()) bp.pop_back();

		ap.push_back(a[p]);
		bp.push_back(b[p]);

		if (ap.size() != bp.size()) break; //说明到此位置结束
	}

	cout << p - 1<< endl; 
	return 0;
}

